﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class EntityExtensions
    {
        public static DynamicEntity ToDynamicEntity(this Entity entity)
        {
            DynamicEntity dynamicEntity = new DynamicEntity(entity.LogicalName);
            foreach(KeyValuePair<String, Object> crm2011Attribute in entity.Attributes)
            {
                Property crm4Property = crm2011Attribute.ToCrm4Property(entity.GetPrimaryKeyAttributeName());
                dynamicEntity.Properties.Add(crm4Property);
            }
            return dynamicEntity;
        }

        public static String GetPrimaryKeyAttributeName(this Entity entity)
        {
            return String.Format("{0}id", entity.LogicalName);
        }
    }
}
